package com.baimo.support.service.impl;

import com.baimo.support.bean.BaseBean;
import com.baimo.support.commons.constants.Constants;
import com.baimo.support.dao.mapper.AdminMapper;
import com.baimo.support.dao.model.AdminModel;
import com.baimo.support.service.IAdminService;
import com.baimo.support.utils.MD5Utils;
import com.baimo.support.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class IAdminServiceImpl extends BaseService implements IAdminService {



    @Override
    public BaseBean login(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String username = request.getParameter("name");
        String password = request.getParameter("password");
        if(StringUtils.isEmpty(username)) return new BaseBean(Constants.HttpResponseCode.AdminLogin.Admin_NAME_EMPTY);
        if(StringUtils.isEmpty(password)) return new BaseBean(Constants.HttpResponseCode.AdminLogin.Admin_PASSWORD_EMPTY);

        AdminModel adminModel = adminMapper.getAdminByName(username);
        if(adminModel == null) return new BaseBean(Constants.HttpResponseCode.AdminLogin.Admin_NAME_OR_PASSWORD_FAIL);
        if(!adminModel.getUserPassword().equals(MD5Utils.encrypt(password))) return new BaseBean(Constants.HttpResponseCode.AdminLogin.Admin_NAME_OR_PASSWORD_FAIL);

        return new BaseBean(Constants.Global.RESPONSE_YES);
    }
}
